package the.workshop.bu.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import the.workshop.bu.dto.CheckTelDTO;
import the.workshop.bu.dto.CheckTelQueryDTO;
import the.workshop.bu.entity.CustomerTel;
import the.workshop.bu.mapper.CustomerTelMapper;
import the.workshop.bu.service.ICustomerTelService;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lei.liu
 * @since 2020-06-15
 */
@Service
public class CustomerTelServiceImpl extends AbstractSuperService<CustomerTelMapper, CustomerTel> implements ICustomerTelService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setLegalTel(Integer id, String modifiedId, String modifiedName) {

        CustomerTel entity = baseMapper.selectById(id);
        if (entity == null) {
            return;
        }

        entity.setIsLegal(0);
        entity.setModifiedTime(new Date());
        entity.setModifiedId(modifiedId);
        entity.setModifiedName(modifiedName);
        baseMapper.updateByCustomerId(entity);

        entity.setIsLegal(1);
        baseMapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByCustomerId(Integer customerId, String modifiedId, String modifiedName) {
        baseMapper.deleteByCustomerId(customerId, new Date(), modifiedId, modifiedName);
    }

    @Override
    public List<CheckTelDTO> listCheckTel(CheckTelQueryDTO query) {
        return this.baseMapper.listCheckTel(query);
    }

    @Override
    public boolean removeById(Serializable id) {
        LambdaUpdateWrapper<CustomerTel> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.set(CustomerTel::getIsDeleted, 1).eq(CustomerTel::getId, id);
        return super.update(updateWrapper);
    }
}
